package probe.sound.code;

import java.io.FileInputStream;
import java.io.InputStream;

public class OpenWav {
  public static void main(String[] args) throws Exception {
    InputStream wavIn
    
    //= Resources.class.getResourceAsStream( "file48000.wav" );
    = new FileInputStream( "/home/pompei/tmp/file.wav" );
    
    byte header[] = new byte[44];
    int bytesForHeader = wavIn.read( header );
    
    if (bytesForHeader != header.length) {
      wavIn.close();
      throw new RuntimeException( "No header: size = " + bytesForHeader );
    }
    
    int dataSize = Util.readInt4( header, 40, true );
    int sampleRate = Util.readInt4( header, 24, true );
    int numChannels = Util.readWord( header, 22, true );
    int bitsPerSample = Util.readWord( header, 34, true );
    int blockAlign = Util.readWord( header, 32, true );
    
    System.out.println( "dataSize = " + dataSize );
    System.out.println( "sampleRate = " + sampleRate );
    System.out.println( "numChannels = " + numChannels );
    System.out.println( "bitsPerSample = " + bitsPerSample );
    System.out.println( "blockAlign = " + blockAlign );
    
    byte[] data = new byte[dataSize];
    
    int readDataSize = wavIn.read( data );
    
    if (readDataSize != data.length) {
      wavIn.close();
      throw new RuntimeException( "Not enaught data: readDataSize = " + readDataSize
          + ", but must be " + data.length );
    }
    
    wavIn.close();
    
    for (int i = 0; i < data.length; i++) {
      System.out.print( Util.asHex( data[i] ) + ' ' );
      if ((i + 1) % 32 == 0) System.out.println();
      if (i > 67) break;
    }
    
    System.out.println( "Complete" );
  }
}
